package com.lsy.poi.exception;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * index 重叠
 *
 */
public class ParseException extends Exception {
	
	private Coordinate coordinate;

	private static final long serialVersionUID = 6523009635351567340L;
	
	@Override
	public String getMessage() {
		return coordinate + " 有误：" + super.getMessage();
	}
	
	/**
	 * 构造方法
	 */
	public ParseException(XSSFCell cell, String message) {
		super(message);
		coordinate = new Coordinate(cell);
	}
	public ParseException(XSSFRow row, String message) {
		super(message);
		coordinate = new Coordinate(row);
	}
	public ParseException(XSSFSheet sheet, String message) {
		super(message);
		coordinate = new Coordinate(sheet);
	}
	public ParseException(XSSFWorkbook workbook, String message) {
		super(message);
		coordinate = new Coordinate(workbook);
	}
	
	
	/**
	 * 内部类：坐标
	 *
	 */
	class Coordinate {
		private String sheetName;
		private int rowIndex;
		private int cellIndex;
		
		private String coordinateStr;

		public Coordinate(XSSFCell cell) {
			sheetName 	= cell.getSheet().getSheetName();
			rowIndex 	= cell.getRowIndex();
			cellIndex 	= cell.getColumnIndex();
			
			coordinateStr = String.format(
								"'%s 页' → '第 %s 行'  → '第 %s 列' 的 单元格 ",
								sheetName, rowIndex, cellIndex);  
		}
		public Coordinate(XSSFRow row) {
			sheetName 	= row.getSheet().getSheetName();
			rowIndex 	= row.getRowNum();
			
			coordinateStr = String.format(
								"'%s 页' → '第 %s 行' ",
								sheetName, rowIndex);  
		}
		public Coordinate(XSSFSheet sheet) {
			sheetName 	= sheet.getSheetName();
			
			coordinateStr = String.format(
								"'%s 页' ",
								sheetName);  
		}
		
		public Coordinate(XSSFWorkbook workbook) {
			coordinateStr = "当前 工作簿";  
		}
		
		
		@Override
		public String toString() {
			return coordinateStr;
		}
		
	}
}